<?php

/*
 * Action - Attack 3$
 * 
 * +1 Card
 * +1 Action
 * Each other player discards down to 4 cards in hand.
 * When you play another Attack card with this in play, you may trash this. If you do, gain a Mercenary from the Mercenary pile.
 *
 */

class Urchin extends Card implements iAction, iKingdom, iDarkAges, iReactingCard, iAttack {

    public function __construct() {
        parent::__construct("Urchin", 3);
        $this->undoPlay = false;
        $this->undoBuy = true;
        
        $this->reactConditions = new ReactionConditions();
        $this->reactConditions->action = array(Attack);
        $this->reactConditions->actor = array(Yourself);
        $this->reactConditions->cardLocation = array(PlayArea);
        $this->reactConditions->limit = ForEachCard;
        $this->reactConditions->isMandatory = false;
    }

    public function getDescription() {
        return array(
            "+1 Card",
            "+1 Action",
            "Each other player discards down to 4 cards in hand.",
            "When you play another Attack card with this in play, you may trash this. If you do, gain a Mercenary from the Mercenary pile.");
    }

    public function getConditions() {
        return $this->reactConditions;
    }

    public function isReactRelevant(Game $game, $card, $name) {
        return true;
    }

    public function react(Game $game, $card, $name) {
        ScreenDisplay::addScreenMessage($game, true, "activate " . nicePrint($this), "activates " . nicePrint($this), $name);
        $options = array(new Option(1, "Yes"), new Option(2, "No"));
        $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Trash Urchin for mercenary card?", $name);
        $answer = $output[0];
        if ($answer == "Yes") {
            $game->moveCardFromPileToPile($this, PlayArea, Trash, $name);
            $mer = $game->moveCardFromPileToPile(null, Mercenary, DiscardPile, $name);
            ScreenDisplay::addScreenMessage($game, true, "trash ".  nicePrint($this)." and gain " . nicePrint($mer), "trashes ".  nicePrint($this)." and gains " . nicePrint($mer), $name);
        } else {
                    ScreenDisplay::addScreenMessage($game, true, "do nothing ", "does nothing", $name);
        }
        $game->setTempCard($card);
        return true;
    }

    public function play(Game $game, $isRealCard) {
        $drawed_cards = $game->drawCards(1);
        $game->addActions(1);
        ScreenDisplay::addScreenMessage($game, true, "get +1 action  and draw " . nicePrint($drawed_cards), "gets +1 action and draws " . sizeof($drawed_cards) . " cards");
        $game->setPhase(Phase_Sleep);

        $game->attack($this);

        $names = $game->getOtherPlayersNames(true);
        foreach ($names as $name) {
            $playHandOther = $game->getPlayHand($name);
            $playHandSize = sizeof($playHandOther);
            if ($playHandSize > 4) {

                $cards_to_discard = $playHandSize - 4;

                $output = MySocket::GetUserReply($game, Input_ChooseMany, $playHandOther, $cards_to_discard, $cards_to_discard, null, "Choose $cards_to_discard cards to discard", $name, true);
            }
        }

        //the waiting function
        $users_replies = MySocket::getUsersReplies($game);
        foreach ($users_replies as $name => $output) {

            foreach ($output as $card_name) {
                $game->moveCardFromPileToPile($card_name, PlayHand, DiscardPile, $name);
            }

            ScreenDisplay::addScreenMessage($game, true, "discard " . nicePrint($output), "discards " . sizeof($output) . " cards", $name);

            $game->setPhase(Phase_Sleep, $name);
        }

        $game->cardActionFinished();
        return true;
    }

    public function boardSetup(Game $game) {
        $game->addMercenary();
        return true;
    }

}

?>